package net.wanglu.www.spdr.digger;

import java.util.ArrayList;
import java.util.HashMap;

import net.wanglu.www.spdr.dao.CorpusFileDAO;
import net.wanglu.www.spdr.entities.Corpus;
import net.wanglu.www.spdr.filter.GuardianNodeFilterFactory;
import net.wanglu.www.spdr.filter.NodeFilterFactory;
import net.wanglu.www.spdr.fixer.GuardianFixer;
import net.wanglu.www.spdr.rule.GuardianRule;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.SimpleNodeIterator;

public class GuardianDigger extends DiggerTemplate {

	@Override
	protected String digAbstract(String page) throws Exception {
		
		GuardianRule rule = (GuardianRule) this.getRule();
		if ( rule == null) rule = new GuardianRule();
		HashMap<String, String> abstractTagRule = rule.abstractTagRule();
		
		NodeFilterFactory factory = this.getNodeFilterFactory();
		if ( factory == null) factory = new GuardianNodeFilterFactory();
		NodeFilter nodeFilter = factory.createNodeFilder(abstractTagRule);
		
		Parser parser = Parser.createParser(page, rule.charSet());
		NodeList nodeList = parser.parse(nodeFilter);
		SimpleNodeIterator e = nodeList.elements(); 
		if ( !e.hasMoreNodes() ) return null;
		Node node = e.nextNode();
		return node.toPlainTextString().trim();
	}

	@Override
	protected String digAuthor(String page) throws Exception {
		
		GuardianRule rule = (GuardianRule) this.getRule();
		if ( rule == null) rule = new GuardianRule();
		HashMap<String, String> authorTagRule = rule.authorTagRule();
		
		NodeFilterFactory factory = this.getNodeFilterFactory();
		if ( factory == null) factory = new GuardianNodeFilterFactory();
		NodeFilter nodeFilter = factory.createNodeFilder(authorTagRule);
		
		Parser parser = Parser.createParser(page, rule.charSet());
		NodeList nodeList = parser.parse(nodeFilter);
		SimpleNodeIterator e = nodeList.elements(); 
		if ( !e.hasMoreNodes() ) return "";
		Node node = e.nextNode();
		return node.toPlainTextString().trim();
	}

	@Override
	protected String digDate(String page) throws Exception {
		
		GuardianRule rule = (GuardianRule) this.getRule();
		if ( rule == null) rule = new GuardianRule();
		HashMap<String, String> dateTagRule = rule.dateTagRule();
		
		NodeFilterFactory factory = this.getNodeFilterFactory();
		if ( factory == null) factory = new GuardianNodeFilterFactory();
		NodeFilter nodeFilter = factory.createNodeFilder(dateTagRule);
		
		Parser parser = Parser.createParser(page, rule.charSet());
		NodeList nodeList = parser.parse(nodeFilter);
		SimpleNodeIterator e = nodeList.elements(); 
		if ( !e.hasMoreNodes() ) return null;
		TagNode node = (TagNode) e.nextNode();
		return node.getAttribute("datetime");
	}

	@Override
	protected ArrayList<String> digText(String page) throws Exception {
			
		GuardianRule rule = (GuardianRule) this.getRule();
		if ( rule == null) rule = new GuardianRule();
		HashMap<String, String> textTagRule = rule.textTagRule();
		
		NodeFilterFactory factory = this.getNodeFilterFactory();
		if ( factory == null) factory = new GuardianNodeFilterFactory();
		NodeFilter nodeFilter = factory.createNodeFilder(textTagRule);

		Parser parser = Parser.createParser(page, rule.charSet());
		NodeList nodeList = parser.parse(nodeFilter);
		if ( nodeList.size() == 0 ) return null;
		
		ArrayList<String> text = new ArrayList<String>(128);
		for (SimpleNodeIterator e = nodeList.elements(); e.hasMoreNodes(); ) {
			Node node = e.nextNode();
			String plainText = node.toPlainTextString();
			if ( plainText == null || plainText.trim().length() == 0 ) continue;
			text.add(plainText.trim());
		}

		return text;
	}

	@Override
	protected String digTitle(String page) throws Exception {
		
		GuardianRule rule = (GuardianRule) this.getRule();
		if ( rule == null) rule = new GuardianRule();
		HashMap<String, String> titleTagRule = rule.titleTagRule();
		
		NodeFilterFactory factory = this.getNodeFilterFactory();
		if ( factory == null) factory = new GuardianNodeFilterFactory();
		NodeFilter nodeFilter = factory.createNodeFilder(titleTagRule);
		
		Parser parser = Parser.createParser(page, rule.charSet());
		NodeList nodeList = parser.parse(nodeFilter);
		SimpleNodeIterator e = nodeList.elements(); 
		if ( !e.hasMoreNodes() ) return null;
		Node node = e.nextNode();
		return node.toPlainTextString().trim();
	}

	@Override
	protected Corpus fixCorpus(Corpus c) throws Exception {
		// TODO Auto-generated method stub
		GuardianFixer fixer = (GuardianFixer) this.getFixer();
		if ( fixer == null ) fixer = new GuardianFixer();
		fixer.fix(c);
		return c;
	}

	public static void main(String args[]) {
		GuardianDigger d = new GuardianDigger();
		try {
			HashMap<String, Object> r = d.digTarget("http://www.guardian.co.uk/sport/gallery/2012/jun/14/olympic-torch-relay-st-andrews-edinburgh");
			Corpus c = d.buildCorpus(r);
			d.fixCorpus(c);
			CorpusFileDAO cf = new CorpusFileDAO();
			cf.insertCorpus(c);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
